package com.googlecode.stockhamster.client.swing;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Date;

import javax.swing.JComponent;

import com.googlecode.stockhamster.stockstat.IStockStat;

public class HistoryView extends JComponent {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2628561451704721129L;

	public static final int BORDER_LEFT = 70, BORDER_RIGHT = 70,
			BORDER_TOP = 70, BORDER_BOTTOM = 70;
	public static final int INNER_BORDER = 30;
	public static final int MEASURE_AXIS_NUM_V = 5;
	public static final int MEASURE_AXIS_NUM_H = 10;
	public static final int HISTORY_NUM = 30;

	private IStockStat stat;
	private Long history[];
	private String stockName;
	boolean PRINT_BAR = true;
	boolean PRINT_LINE = true;

	public HistoryView(IStockStat stat) {
		this.stat = stat;
		history = new Long[1];
		history[0] = 0L;
		stockName = "";
	}

	public void showStock(String stockName, Boolean print_bar,
			Boolean print_line) {
		Integer stockId = stat.getStockId(stockName);
		history = stat.getHistory(stockId);
		this.stockName = stockName;
		this.PRINT_BAR = print_bar;
		this.PRINT_LINE = print_line;
		repaint();
	}

	@Override
	public void paint(Graphics g) {
		int height = this.getHeight();
		int width = this.getWidth();
		int height_g = this.getHeight() - BORDER_TOP - BORDER_BOTTOM;
		int width_g = this.getWidth() - BORDER_LEFT - BORDER_RIGHT;

		Long min = history[0];
		Long max = history[0];
		for (Long l : history) {
			max = Math.max(max, l);
			min = Math.min(min, l);
		}

		int scale_h = Math.min(HISTORY_NUM, history.length);
		Long scale_v = Math.max(max - min, Math.max(Math.abs(min), max)) *3/2;
		Double value_v = (double) height_g / (double) scale_v;
		Double value_h = (double) width_g / (double) scale_h;

		// background
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, width, height);

		// stock name
		g.setColor(Color.WHITE);
		g.drawString(stockName, width / 2, BORDER_TOP / 2);

		//axis
		g.setColor(Color.WHITE);
		g.drawLine(BORDER_LEFT - INNER_BORDER, BORDER_TOP - INNER_BORDER, 
				BORDER_LEFT - INNER_BORDER, BORDER_TOP + height_g + INNER_BORDER + 10);
		g.drawLine(BORDER_LEFT - 10 - INNER_BORDER, BORDER_TOP + height_g + INNER_BORDER, 
				BORDER_LEFT + width_g + INNER_BORDER, BORDER_TOP + height_g + INNER_BORDER);

		//measure
		g.setColor(Color.GRAY);
		for (int i = 0; i <= MEASURE_AXIS_NUM_V + 1; i ++){
			int h = BORDER_TOP + i * height_g / (MEASURE_AXIS_NUM_V + 1);
			g.drawLine(BORDER_LEFT - INNER_BORDER, h, 
					BORDER_LEFT + INNER_BORDER + width_g, h);
		}

		//axis value v
		g.setColor(Color.WHITE);
		for (int i = 0; i <= MEASURE_AXIS_NUM_V + 1; i ++){
			int h = BORDER_TOP + i * height_g / (MEASURE_AXIS_NUM_V + 1);
			g.drawString("" + scale_v / (MEASURE_AXIS_NUM_V + 1) * (MEASURE_AXIS_NUM_V - i + 1), 
					BORDER_LEFT - INNER_BORDER - 20, h);
		}

		//server time
		g.setColor(Color.WHITE);
		Date currTime = new Date(System.currentTimeMillis());
		g.drawString(currTime.toString(), BORDER_TOP + width_g - 150, BORDER_TOP);
		
		long value_t = scale_h / (MEASURE_AXIS_NUM_H + 1) * 60000;
		g.setColor(Color.WHITE);
		for (int i = 0; i <= MEASURE_AXIS_NUM_H + 1; i ++){
			Date axisTime = new Date(System.currentTimeMillis() - value_t * i);
			int w = width - BORDER_RIGHT - i * width_g / (MEASURE_AXIS_NUM_H + 1);
			g.drawString("" + axisTime.getHours() + ":" + axisTime.getMinutes(), 
					w, BORDER_TOP + INNER_BORDER + height_g + 20);
		}

		if (PRINT_BAR)
			for (int i = 0; i < scale_h - 1; i++) {
				int x1 = width - BORDER_RIGHT - (int) (i * value_h);
				int y1 = (int) (height - history[i] * value_v - BORDER_BOTTOM);
				int x2 = width - BORDER_RIGHT - (int) ((i + 1) * value_h);
				int y2 = (int) (height - history[i + 1] * value_v - BORDER_BOTTOM);
				if (y1 > y2)
					g.setColor(Color.GREEN);
				else
					g.setColor(Color.RED);
				g.fillRect(x2, Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
			}

		if (PRINT_LINE)
			for (int i = 0; i < scale_h - 1; i++) {
				int x1 = width - BORDER_RIGHT - (int) (i * value_h);
				int y1 = (int) (height - history[i] * value_v - BORDER_BOTTOM);
				int x2 = width - BORDER_RIGHT - (int) ((i + 1) * value_h);
				int y2 = (int) (height - history[i + 1] * value_v - BORDER_BOTTOM);
				if (!PRINT_BAR) {
					if (y1 > y2)
						g.setColor(Color.GREEN);
					else
						g.setColor(Color.RED);
				} else {
					if (y1 > y2)
						g.setColor(Color.BLUE);
					else
						g.setColor(Color.YELLOW);
				}
				g.drawLine(x1, y1, x2, y2);
			}

	}
}
